mui.init();
function go_back() {
	window.history.go(-1);
}
var clock = '';
var nums = 60;
var btn;

function get_code(thisBtn) {

	var username = $("#username").val();
	var phone = $("#doc-vld-528").val();
	var myreg = /^(((13[0-9]{1})|159)+\d{8})$/;
	if($("#doc-vld-528").val() == "") {
		mui.alert("手机号不能为空!");
		$("#doc-vld-528").focus();
		return false;
	} else if($("#doc-vld-528").val().length != 11 && !myreg.test($("#doc-vld-528").val())) {
		mui.alert("手机号格式不正确!");
		$("#doc-vld-528").focus();
		return false;
	}
	btn = thisBtn;
	btn.disabled = true; //将按钮置为不可点击
	btn.innerText = nums + '秒后获取';
	clock = setInterval(doLoop, 1000); //一秒执行一次
	$.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
		$.ajax({
			type: "post",
			cache: false,
			url: locat + '/appClient/getPhoneCode',
			timeout: 30000,
			xhrFields: {
				withCredentials: true,
				useDefaultXhrHeader: false
			},
			crossDomain: true,
			data: {
				'phone': phone,
				'username': username,
				datetime: new Date().getTime()
			},
			dataType: "json",
			success: function(data) {
				if("success" == data["msg"]) {
					document.getElementById("wx_id").value = data["wx_id"];
					mui.alert("验证码发送成功");
				} else {
					mui.alert("验证码发送失败，请稍后重新获取。");
				}

			}
		});
	});
}

function save_pwd() {
	var phone  = $("#doc-vld-528").val();
	var username = $("#username").val();
	var wx_id = $("#wx_id").val();
	var code = $("#code").val();
	var password = $("#doc-vld-pwd-1").val();
	var checkpassword = $("#doc-vld-pwd-2").val();

	if($("#doc-vld-528").val() == "") {
		mui.alert("手机号不能为空!");
		$("#doc-vld-528").focus();
		return false;
	} else if($("#doc-vld-528").val().length != 11 && !myreg.test($("#doc-vld-528").val())) {
		mui.alert("手机号格式不正确!");
		$("#doc-vld-528").focus();
		return false;
	}
	if($("#code").val() == "") {
		mui.alert("验证码不能为空!");
		$("#code").focus();
		return false;
	}
	if($("#doc-vld-pwd-1").val() == "" || $("#doc-vld-pwd-1").val().length != 6) {
		$("#doc-vld-pwd-1").focus();
		return false;
	}
	if($("#doc-vld-pwd-2").val() == "" || $("#doc-vld-pwd-2").val().length != 6) {
		mui.alert("确认密码不能为空!");
		$("#doc-vld-pwd-2").focus();
		return false;
	}
	if($("#doc-vld-pwd-1").val() != $("#doc-vld-pwd-2").val()) {
		mui.alert("两次密码不相同!");
		$("#doc-vld-pwd-2").focus();
		return false;
	}
	$.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
		var companyid = data["companyid"];
		$.ajax({
			type: "post",
			cache: false,
			url: locat + '/appClient/save_pwd',
			timeout: 10000,
			xhrFields: {
				withCredentials: true,
				useDefaultXhrHeader: false
			},
			crossDomain: true,
			data: {
				"username" : username ,
				"phone": phone,
				"code": code,
				"password": password,
				"companyid": companyid,
				"wx_id": wx_id
			},
			dataType: "json",
			success: function(data) {

				if("success" == data["msg"]) {
					mui.alert( "密码修改成功，您可以登录了");
					window.location.href = "login.html";
				} else if("expiry" == data["msg"]) {
					mui.alert( "验证码过期，请稍后重新获取");
					$("#code").focus();
				} else if("userNo" == data["msg"]){
					$("#username").focus();
					$("#doc-vld-528").focus();
					mui.alert( "用户名，手机号有误。请核实后重新输入。");
				}

			}
		});

	});

}

function doLoop() {
	nums--;
	if(nums > 0) {
		btn.innerText = nums + '秒后获取';
	} else {
		clearInterval(clock); //清除js定时器
		btn.disabled = false;
		btn.innerText = '获取验证码';
		nums = 10; //重置时间
	}
}